package com.k.kc.util.thread.demo1;

import com.k.kc.util.thread.ThreadFactoryBuilder;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class BusinessThreadPool {

    private BusinessThreadPool() {
    }

    private static class SingletonHolder {
        private static final int DEFAULT_CORE_POOL_SIZE = 5;
        private static final int DEFAULT_MAXIMUM_POOL_SIZE = 10;
        private static final long DEFAULT_KEEP_ALIVE_TIME = 60;
        private static final TimeUnit DEFAULT_UNIT = TimeUnit.SECONDS;
        private static final int DEFAULT_CAPACITY = 32;
        private static final String DEFAULT_THREAD_NAME_PREFIX = "kpp-thread-";
        private static final int DEFAULT_THREAD_PRIORITY = 1;


        private static final ExecutorService executorService = new ThreadPoolExecutor(
                DEFAULT_CORE_POOL_SIZE,
                DEFAULT_MAXIMUM_POOL_SIZE,
                DEFAULT_KEEP_ALIVE_TIME,
                DEFAULT_UNIT,
                new LinkedBlockingQueue<>(DEFAULT_CAPACITY),
                new ThreadFactoryBuilder().setNameFormat(DEFAULT_THREAD_NAME_PREFIX).setPriority(DEFAULT_THREAD_PRIORITY).build(),
                new ThreadPoolExecutor.AbortPolicy());
    }

    public static ExecutorService getThreadPool() {
        return SingletonHolder.executorService;
    }

}
